﻿using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.FemaleRen
{
    public class FemaleRenDeleteHandler : IRequestHandler<FemaleRenDeleteCommand, Result>
    {
        private readonly IBaseRepository<EwePregnancyCheckRecord> ewePregnancyCheckRecordRepository;
        private readonly IBaseRepository<SleepInfor> SleepInforbaseRepository;

        public FemaleRenDeleteHandler(IBaseRepository<EwePregnancyCheckRecord> ewePregnancyCheckRecordRepository, IBaseRepository<SleepInfor> SleepInforbaseRepository)
        {
            this.ewePregnancyCheckRecordRepository = ewePregnancyCheckRecordRepository;
            this.SleepInforbaseRepository = SleepInforbaseRepository;
        }
        /// <summary>
        /// 批量删除妊检信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(FemaleRenDeleteCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result();
                string[] strings = request.Ids.Split(',');
                foreach (var item in strings)
                {
                    var info = ewePregnancyCheckRecordRepository.GetAll().FirstOrDefault(x => x.EwePregnancyCheckRecordId == Convert.ToInt32(item));
                    info.Deletes = false;                    
                    result.Code += ewePregnancyCheckRecordRepository.Update(info);
                    if(result.Code > 0)
                    {
                        var res=SleepInforbaseRepository.GetAll().FirstOrDefault(x=>x.SleepInforId==info.EarNo);
                        if(res != null)
                        {
                            res.CheckPre = null;
                            result.Code += SleepInforbaseRepository.Update(res);
                        }
                    }
                }
               
                
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
